﻿using StoreSQLSugar.DBUtility;
using StoreSQLSugar.Helpers;
using StoreSQLSugar.Models;
using StoreSQLSugar.Models.ViewModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StoreSQLSugar
{
    public partial class LoginFrm : Form
    {
        public LoginFrm()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            // 1。校验
            if (string.IsNullOrWhiteSpace(txtAccount.Text))
            {
                MessageBox.Show("请输入账号！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrWhiteSpace(txtPassword.Text))
            {
                MessageBox.Show("请输入密码！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // 2。操作数据库
            var user = SqlSugarHelper.Db.Queryable<VUser>().Where(u => u.Account == txtAccount.Text &&
             u.Password == txtPassword.Text &&
             u.Status == 0).Single();
            if (user == null)
            {
                MessageBox.Show("登录失败，可能账号或密码有误！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // 保存登录信息
            LoginHelper.User = user;

            DialogResult = DialogResult.OK;
        }
    }
}
